package com.changgou.seckill.web.controller;

import com.changgou.entity.Result;
import com.changgou.seckill.feign.SecKillGoodsFeign;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/wseckillgoods")
public class SecKillGoodsController {

    /***
     * 跳转秒杀首页
     * @return
     */
    @GetMapping("/toIndex")
    public String toIndex() {
        return "seckill-index";
    }

    /***
     * 获取秒杀时间段集合信息
     * @return
     */
    @RequestMapping("/timeMenus")
    @ResponseBody
    public List<String> dateMenus() {
        List<Date> dateMenus = DateUtil.getDateMenus();
        List<String> resultList = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Date dateMenu : dateMenus) {
            String format = simpleDateFormat.format(dateMenu);
            resultList.add(format);
        }
        return resultList;
    }

    @Autowired
    private SecKillGoodsFeign secKillGoodsFeign;

    /***
     * 获取秒杀商品列表
     * @param time 默认当前时间
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result<List<SeckillGoods>> list(String time) {
        String timestr = DateUtil.formatStr(time);
        Result<List<SeckillGoods>> listResult = secKillGoodsFeign.list(timestr);
        return listResult;
    }
}
